package com.weight.system.user.web;

import com.weight.system.org.PO.OrgInfo;
import com.weight.system.org.service.OrgInfoService;
import com.weight.system.user.PO.UserInfo;
import com.weight.system.user.VO.UserInfoVO;
import com.weight.system.user.service.UserInfoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 人员信息表
 */
@RestController
@Slf4j
@RequestMapping("/userInfo")
public class UserInfoController {

	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private OrgInfoService orgInfoService;
	private String pattern = "yyyy-MM-dd";


	/**
	 * 1.查询人员信息表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForUserInfoList",method=RequestMethod.GET)
	public ModelAndView queryForUserInfoList(){
		Map<Object, Object> map =new HashMap<>();
		map.put("userStatus", 1);
	 
		//2、查询人员信息表列表
		List<UserInfo> userInfoList = userInfoService.findUserInfoByT(map);
		log.info("userInfoList={}",userInfoList);
		List<UserInfoVO> userInfoVOList = userInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user/queryForUserInfoList");
		modelAndView.addObject("userInfoVOList",userInfoVOList);
		return modelAndView;
}
	/**
	 * 3.添加、修改人员信息表操作
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/addUserInfo")
	public ModelAndView addUserInfo(@RequestParam(value="userId",required=false) String userId){
			//1、创建返回对象
			UserInfoVO userInfoVO = new UserInfoVO();
			
			//2.判定id不为空，则查询数据库，为修改操作
			if(StringUtils.isNotBlank(userId)) {
				Map<String, String> map=new HashMap<>();
				map.put("userId", userId);
				log.info("修改操作，查询ID：userId={}",userId);
				UserInfo userInfo = userInfoService.findByUserInfoId(map);
				this.changePoToVO(userInfoVO,userInfo);
			}
			//2、查询组织机构信息表列表
			Map<Object, Object> map =new HashMap<>();
			map.put("standbyfield1", "1");
			List<OrgInfo> orgInfoList = orgInfoService.findOrgInfoByT(map);
			//3.数据返回给前台
			ModelAndView modelAndView = new ModelAndView();
			modelAndView.setViewName("user/addUserInfo");
			modelAndView.addObject("userInfoVO",userInfoVO);
			modelAndView.addObject("orgInfoList",orgInfoList);
			return modelAndView;
	}

	/**
	 * 5.保存 添加、修改人员信息表操作
	 * @param userInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveUserInfo")
	public JsonResultUtil saveUserInfo(@Valid UserInfoVO userInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,userInfoVO);
			
			//2.获取id
			String userId=userInfoVO.getUserId();
			UserInfo userInfo=new UserInfo();
			BeanUtils.copyProperties(userInfoVO,userInfo);
			this.changeVOToPO(userInfoVO,userInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(userId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("userId", userId);
				log.info("修改操作，查询ID：userId={}",userId);
				//查询是否存在
				userInfoService.findByUserInfoId(map);
				try{
					userInfo.setUpdateBy("000");
					userInfoService.updateUserInfo(userInfo);
					log.info("更新操作成功：userInfo={}",userInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					userInfo.setUserId(CommonUtils.getUUId());
					userInfo.setCreateBy("000");
					userInfo.setUpdateBy("000");
					userInfo.setUserStatus(1);
					userInfoService.insertUserInfo(userInfo);
					log.info("添加操作成功：userInfo={}",userInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 2.查询人员信息表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForUserInfoListDG",method=RequestMethod.GET)
	public String queryForUserInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String userName = request.getParameter("userName");
			String userCode = request.getParameter("userCode");
			String userSex = request.getParameter("userSex");
			String userBirth = request.getParameter("userBirth");
			String userIdnum = request.getParameter("userIdnum");
			String userTel = request.getParameter("userTel");
			String userAddress = request.getParameter("userAddress");
			String userPcode = request.getParameter("userPcode");
			String userStatus = request.getParameter("userStatus");
			String userPath = request.getParameter("userPath");

			Map<Object, Object> map =new HashMap<>();
			map.put("userName", userName);
			map.put("userCode", userCode);
			map.put("userSex", userSex);
			map.put("userBirth", userBirth);
			map.put("userIdnum", userIdnum);
			map.put("userTel", userTel);
			map.put("userAddress", userAddress);
			map.put("userPcode", userPcode);
			map.put("userStatus", userStatus);
			map.put("userPath", userPath);

			
			//2、查询人员信息表列表
			List<UserInfo> userInfoList = userInfoService.findUserInfoByPage(page,rows,map);
			log.info("userInfoList={}",userInfoList);
			List<UserInfoVO> userInfoVOList = userInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<UserInfoVO> result = new PageResult<UserInfoVO>(userInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param userInfoVO
	 * @param userInfo
	 * @return
	 */
	public void changeVOToPO(UserInfoVO userInfoVO,UserInfo userInfo){
			BeanUtils.copyProperties(userInfoVO,userInfo);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param userInfoVO
	 * @param userInfo
	 * @return
	 */
	public void changePoToVO(UserInfoVO userInfoVO,UserInfo userInfo){
			BeanUtils.copyProperties(userInfo,userInfoVO);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param userInfo
	 * @return
	 */
	public UserInfoVO changePoToVO(UserInfo userInfo){
			UserInfoVO userInfoVO=new UserInfoVO();
			BeanUtils.copyProperties(userInfo,userInfoVO);
			

			return userInfoVO;
	}

	/**
	 * 3.查询人员信息表--详情页
	 * @param userId
	 * @return
	 */
	@RequestMapping("/queryForUserInfoDetail")
	public ModelAndView queryForUserInfoDetail(@RequestParam(value="userId",required=false) String userId){
			//1.校验id
			CommonUtils.validateExistID(userId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("userId", userId);
			log.info("查询人员信息表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			UserInfo userInfo = userInfoService.findByUserInfoId(map);
			
			UserInfoVO userInfoVO = changePoToVO(userInfo);
			//3、返回前台
			log.info("查询人员信息表查询成功：userInfo={}",userInfo);
			modelAndView.setViewName("system/user/queryForUserInfoDetail");
			modelAndView.addObject("userInfoVO",userInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	
	/**
	 * 6.删除人员信息表操作
	 * @param userId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByUserInfoId")
	public JsonResultUtil deleteByUserInfoId(@RequestParam(value="userId",required=false) String userId){
			//1.校验id
			CommonUtils.validateExistID(userId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("userId", userId);
			userInfoService.findByUserInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除人员信息表入参，map={}",map);
			
			try{
				userInfoService.upDelByUserInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除人员信息表操作---批量删除
	 * @param userIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByUserInfoIdBatch")
	public JsonResultUtil deleteByUserInfoIdBatch(@RequestParam(value="userIdList",required=false) List userIdList){
			//1.校验id
			CommonUtils.validateExistIDList(userIdList);
			
			//2.校验数据库
			userInfoService.findByUserInfoIdList(userIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("userIdList", userIdList);
			map.put("updateBy","000");
			log.info("批量删除人员信息表操作入参，map={}",map);
			
			try{
				userInfoService.upDelByUserInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}